﻿#include <QDebug>
#include <QScrollBar>
#include <QTimer>

#include "CPlcMainWidget.h"
#include "ui_CPlcMainWidget.h"
//#include "CPlcCenterWidget.h"
#include "ui_CPlcCenterWidget.h"




CPlcMainWidget::CPlcMainWidget(QWidget *parent)
    : QWidget(parent), ui(new Ui::CPlcMainWidget)
{
    ui->setupUi(this);
    ui->widget->hide();
    ui->widgetEdit->hide();
    //初始化界面指针
    editWidget = new CPlcItemEditWidget(this);
    findWidget = new CPlcItemFindWidget(this);

    this->setFixedSize(640, 400);
    //方便在构造函数中获取控件真实大小
    this->show();
    this->hide();

    initUi();

    //以下是测试代码
//    Ui::CPlcCenterWidget *center = new Ui::CPlcCenterWidget;
//    center->setupUi(this);

//    CPlcSceneView *sceneView = new CPlcSceneView();
//    sceneView->setScene(&mScene);

//    center->editLayout->addWidget(editWidget);
//    center->viewLayout->addWidget(sceneView);
//	editWidget->show();
//	sceneView->show();
//    this->show();
	

}

CPlcMainWidget::~CPlcMainWidget()
{
    delete editWidget;
    delete findWidget;
    delete ui;
}

//周期刷新函数
void CPlcMainWidget::loopRefresh()
{
    //垂直滚动条保持显示
    double width = ui->graphicsView->size().width() -
                   ui->graphicsView->verticalScrollBar()->width() - 2;
    mScene.updateSceneWidth(width);
}

//初始化
void CPlcMainWidget::initUi()
{
    //子界面插入
    ui->glEditWidget->setContentsMargins(
        0, 0, 0, 0);   //设置网格布局边缘留白：左右上下像素
    ui->glEditWidget->addWidget(editWidget, 0, 0, 1, 1);
    ui->glEditWidget->addWidget(findWidget, 0, 0, 1, 1);

#if 1
    connect(ui->tb1, SIGNAL(clicked()), this, SLOT(slotOnOpenContact()));
    connect(ui->tb2, SIGNAL(clicked()), this, SLOT(slotOnCloseContact()));
    connect(ui->tb3, SIGNAL(clicked()), this, SLOT(slotOnPulse()));
    connect(ui->tb4, SIGNAL(clicked()), this, SLOT(slotOnFalling()));
    connect(ui->tb5, SIGNAL(clicked()), this, SLOT(slotOnSet()));
    connect(ui->tb6, SIGNAL(clicked()), this, SLOT(slotOnReset()));
    connect(ui->tb7, SIGNAL(clicked()), this, SLOT(slotOnCoil()));
    connect(ui->tb8, SIGNAL(clicked()), this, SLOT(slotOnFunctionBlock()));
    connect(ui->tb9, SIGNAL(clicked()), this, SLOT(slotOnBranchDown()));
    connect(ui->tb10, SIGNAL(clicked()), this, SLOT(slotOnLinkLine()));
    connect(ui->pbInsertRow, SIGNAL(clicked()), this, SLOT(slotInseretRow()));
    connect(ui->pbDeleteRow, SIGNAL(clicked()), this, SLOT(slotDeleteRow()));
    connect(ui->pbDeleteSub, SIGNAL(clicked()), this,
            SLOT(slotRemoveBranchDown()));
    connect(ui->pbaddItem, SIGNAL(clicked()), this, SLOT(slotInseretItem()));
    connect(ui->pbDeleteItem, SIGNAL(clicked()), this, SLOT(slotDeleteItem()));

    connect(ui->pb1, SIGNAL(clicked()), this, SLOT(slotOnFindItem()));
    connect(ui->pb2, SIGNAL(clicked()), this, SLOT(slotOnEditItem()));
    connect(ui->pb3, SIGNAL(clicked()), this, SLOT(slotOnRedo()));
    connect(ui->pb4, SIGNAL(clicked()), this, SLOT(slotOnCompile()));
    connect(ui->pb5, SIGNAL(clicked()), this, SLOT(slotOnUndo()));
    connect(ui->pb6, SIGNAL(clicked()), this, SLOT(slotOnCheckView()));
    connect(ui->pb7, SIGNAL(clicked()), this, SLOT(slotOnEidtConfirm()));
    connect(ui->pb8, SIGNAL(clicked()), this, SLOT(slotOnEidtCancel()));
    connect(ui->pb9, SIGNAL(toggled(bool)), this,
            SLOT(slotSetCommentVisible(bool)));
    connect(ui->pb10, SIGNAL(clicked()), this, SLOT(slotOnZoomIn()));
    connect(ui->pb11, SIGNAL(clicked()), this, SLOT(slotOnZoomOut()));
    connect(ui->pb12, SIGNAL(toggled(bool)), this, SLOT(slotOnMonitor(bool)));

#endif

    //垂直滚动条保持显示
    double width = ui->graphicsView->size().width() -
                   ui->graphicsView->verticalScrollBar()->width() - 2;
    double high = ui->graphicsView->size().height() - 2;
    mScene.setSceneRect(QRectF(0, 0, width, high));
    mScene.initScene();
    mScene.updateSceneWidth(width);

    ui->graphicsView->setScene(&mScene);
    ui->graphicsView->centerOn(100, 100);
}

//监视
void CPlcMainWidget::slotOnMonitor(bool value)
{
    if (value)
    {
        mScene.switchToMonitorLad();
    }
    else
    {
        mScene.switchToEditLad();
    }
}

//编译
void CPlcMainWidget::slotOnCompile()
{
    mScene.compileLad();
}

//修改
void CPlcMainWidget::slotOnEditItem()
{
    SItemData itemData;
    //获取失败时
    if (!mScene.getSelectItemData(itemData))
    {
        ui->widgetEdit->hide();
    }
    else
    {
        findWidget->hide();
        editWidget->show();
        editWidget->setDataToUi(itemData);
        ui->widget->show();
        ui->widgetEdit->show();
    }
}

//编辑取消
void CPlcMainWidget::slotOnEidtCancel()
{
    ui->widgetEdit->hide();
}

//编辑确认
void CPlcMainWidget::slotOnEidtConfirm()
{
    //查找
    if (findWidget->isVisible())
    {
        QString variable = findWidget->getVariable();
        mScene.findItem(variable);
        return;
    }

    if (!ui->widgetEdit->isVisible()) return;
    SItemData itemData = editWidget->getDataFromUi();
    mScene.replaceItem(itemData);
    ui->widgetEdit->hide();
}

//查找
void CPlcMainWidget::slotOnFindItem()
{
    if (!findWidget->isVisible())
    {
        findWidget->show();
        editWidget->hide();
        ui->widget->show();
        ui->widgetEdit->show();
    }
    else
    {
        findWidget->hide();
        editWidget->hide();
        ui->widget->hide();
    }
}

//查看和编辑页切换
void CPlcMainWidget::slotOnCheckView()
{
    //判断右边元件页是否显示
    if (ui->widget->isVisible())
    {
        ui->widget->hide();
        ui->widgetEdit->hide();
    }
    else
    {
        ui->widget->show();
    }
}
//撤销
void CPlcMainWidget::slotOnUndo()
{
    mScene.onUndo();
}

//还原
void CPlcMainWidget::slotOnRedo()
{
    mScene.onRedo();
}

//放大
void CPlcMainWidget::slotOnZoomIn()
{
    mScene.onZoomIn();
}

//缩小
void CPlcMainWidget::slotOnZoomOut()
{
    mScene.onZoomOut();
}

//设置注释可见性
void CPlcMainWidget::slotSetCommentVisible(bool value)
{
    mScene.setCommentView(value);
}

//常开按钮触发函数
void CPlcMainWidget::slotOnOpenContact()
{
    SItemData itemData;
    memset(&itemData.component, 0, sizeof(SComponent));
    itemData.pItem          = NULL;
    itemData.pComment       = NULL;
    itemData.component.type = plc::OpenContact;
    itemData.component.name = plc::Input_X;
    editWidget->setDataToUi(itemData);
    editWidget->show();
    findWidget->hide();
    ui->widgetEdit->show();
}
//常闭按钮触发函数
void CPlcMainWidget::slotOnCloseContact()
{
    SItemData itemData;
    memset(&itemData.component, 0, sizeof(SComponent));
    itemData.pItem          = NULL;
    itemData.pComment       = NULL;
    itemData.component.type = plc::CloseContact;
    itemData.component.name = plc::Input_X;
    editWidget->setDataToUi(itemData);
    editWidget->show();
    findWidget->hide();
    ui->widgetEdit->show();
}
//上升沿按钮触发函数
void CPlcMainWidget::slotOnPulse()
{
    SItemData itemData;
    memset(&itemData.component, 0, sizeof(SComponent));
    itemData.pItem          = NULL;
    itemData.pComment       = NULL;
    itemData.component.type = plc::Pulse;
    itemData.component.name = plc::Input_X;
    editWidget->setDataToUi(itemData);
    editWidget->show();
    findWidget->hide();
    ui->widgetEdit->show();
}
//下降沿按钮触发函数
void CPlcMainWidget::slotOnFalling()
{
    SItemData itemData;
    memset(&itemData.component, 0, sizeof(SComponent));
    itemData.pItem          = NULL;
    itemData.pComment       = NULL;
    itemData.component.type = plc::Falling;
    itemData.component.name = plc::Input_X;
    editWidget->setDataToUi(itemData);
    editWidget->show();
    findWidget->hide();
    ui->widgetEdit->show();
}
//线圈按钮触发函数
void CPlcMainWidget::slotOnCoil()
{
    SItemData itemData;
    memset(&itemData.component, 0, sizeof(SComponent));
    itemData.pItem          = NULL;
    itemData.pComment       = NULL;
    itemData.component.type = plc::Coil;
    itemData.component.name = plc::Output_Y;
    editWidget->setDataToUi(itemData);
    editWidget->show();
    findWidget->hide();
    ui->widgetEdit->show();
}
//置位线圈按钮触发函数
void CPlcMainWidget::slotOnSet()
{
    SItemData itemData;
    memset(&itemData.component, 0, sizeof(SComponent));
    itemData.pItem          = NULL;
    itemData.pComment       = NULL;
    itemData.component.type = plc::Set;
    itemData.component.name = plc::Output_Y;
    editWidget->setDataToUi(itemData);
    editWidget->show();
    findWidget->hide();
    ui->widgetEdit->show();
}
//复位线圈按钮触发函数
void CPlcMainWidget::slotOnReset()
{
    SItemData itemData;
    memset(&itemData.component, 0, sizeof(SComponent));
    itemData.pItem          = NULL;
    itemData.pComment       = NULL;
    itemData.component.type = plc::Reset;
    itemData.component.name = plc::Output_Y;
    editWidget->setDataToUi(itemData);
    editWidget->show();
    findWidget->hide();
    ui->widgetEdit->show();
}
//功能块按钮触发函数
void CPlcMainWidget::slotOnFunctionBlock()
{
    SItemData itemData;
    memset(&itemData.component, 0, sizeof(SComponent));
    itemData.pItem          = NULL;
    itemData.pComment       = NULL;
    itemData.component.type = plc::TON;
    itemData.component.name = plc::Timer_T;
    editWidget->setDataToUi(itemData);
    editWidget->show();
    findWidget->hide();
    ui->widgetEdit->show();
}
//分支按钮触发函数
void CPlcMainWidget::slotOnBranchDown()
{
    mScene.addBranch();
}
//连接线按钮触发函数
void CPlcMainWidget::slotOnLinkLine()
{
    SItemData itemData;
    memset(&itemData.component, 0, sizeof(SComponent));
    itemData.pItem          = NULL;
    itemData.pComment       = NULL;
    itemData.component.type = plc::Line;
    mScene.replaceItem(itemData);
}
//删除分支按钮触发函数
void CPlcMainWidget::slotRemoveBranchDown()
{
    mScene.removeBranch();
}
//插入一行按钮触发函数
void CPlcMainWidget::slotInseretRow()
{
    mScene.insertRowForward();
}
//删除一行按钮触发函数
void CPlcMainWidget::slotDeleteRow()
{
    mScene.removeCurrentRow();
}
//插入元件按钮触发函数
void CPlcMainWidget::slotInseretItem()
{
    mScene.insertItem();
}
//删除元件按钮触发函数
void CPlcMainWidget::slotDeleteItem()
{
    mScene.removeItem();
}
